package product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.JLabel;
import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class ChulList extends JInternalFrame {

	MemberMonth2 mm2;
	ProductMain3 pm;
	SemiChul sc;
	
	private JScrollPane scrollPane;
	HashMap<String, ChulGData> chulG;
	HashMap<String, MemberData> mem;

	DefaultTableModel model;
	TableColumn column = null;

	private JTable table;
	private JLabel status;
	private JButton btnNewButton;

	public ChulList(ProductMain3 pm, SemiChul sc) {
		super("출퇴근 리스트", false, true, false, true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				closing();
			}
		});
		this.pm = pm;
		this.sc = sc;
		Vector<String> colNames = new Vector<String>(); // 벡터에 테이블항목을 저장하기위해서
														// colNames 생성.
		colNames.add("사원번호");
		colNames.add("이름");
		colNames.add("날짜");
		colNames.add("근태");

		model = new DefaultTableModel(colNames, 0){
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};

		setBounds(100, 100, 450, 500);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getStatus());
		getContentPane().add(getButton_1());

		for (int i = 0; i < 3; i++) {
			column = table.getColumnModel().getColumn(i);
			if (i == 2) {
				column.setPreferredWidth(202); // sport column is bigger
			} else {
				column.setPreferredWidth(68);
			}
		}

	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 44, 420, 417);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		return table;
	}

	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setFont(new Font("굴림", Font.BOLD, 15));
			status.setBounds(10, 10, 218, 26);
		}
		return status;
	}

	private JButton getButton_1() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("월별");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					pm.openMemberMonth2();
				}
			});
			btnNewButton.setBounds(325, 11, 97, 23);
		}
		return btnNewButton;
	}

	public void dayhistory() {
		try {
			for (int i = table.getRowCount() - 1; i >= 0; i--) {
				model.removeRow(i);
			}
			String sql = "select ih.memberid, md.irum, ih.his_time, ih.inout_type from inouthistory ih, memberdata md where ih.memberid = '"
					+ pm.logincode
					+ "' and substr(ih.his_time, 0, 10) = substr(sysdate, 0, 10) and ih.memberid = md.memberid ";
			PreparedStatement pstmt = pm.conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			String la = "";
			while (rs.next()) {
				Vector r = new Vector<>();
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
					if (i % 4 == 0) {
						if (rs.getString(i).equals("1")) {
							r.add("출근");
						} else {
							r.add("퇴근");
						}
					}
					r.add(rs.getString(i));
				}
				model.addRow(r);
			}
			la = la + "오늘의 출퇴근 기록입니다.";
			rs.close();
			pstmt.close();
			status.setText(la);
			tbc();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	
	public void tbc(){
		table.setDefaultRenderer(Object.class, new myColorRenderer());
	}
	
	public class myColorRenderer extends DefaultTableCellRenderer { 

		 public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,
		                                                boolean hasFocus, int row, int column) 
		 { 
		 
		  Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
		  
		  String s = table.getModel().getValueAt(row, column).toString(); 
		  
		  //if구문의 내용은 테이블에 따라 다르니 적절한 코딩하여야 한다.
		  //참고로 rowIndex, colIndex는 0부터 시작한다.
		  if(s.equalsIgnoreCase("퇴근")){ 
		   comp.setBackground(Color.PINK); 
		   comp.setForeground(Color.BLUE);
		  }
		  else if(s.equalsIgnoreCase("출근")){ 
		   comp.setBackground(Color.YELLOW); 
		   comp.setForeground(Color.RED); 
		  } 
//		  else if(column == 0 && row % 2 == 0){
//			  comp.setBackground(Color.CYAN);
//			  comp.setForeground(Color.ORANGE);
//		  }
//		  else if(column == 0 && row % 2 != 0){
//			  comp.setBackground(Color.BLACK);
//			  comp.setForeground(Color.WHITE);
//		  }
//		  else if(column == 1 && row % 2 == 0){
//			  comp.setBackground(Color.MAGENTA);
//			  comp.setForeground(Color.ORANGE);
//		  }
//		  else if(column == 1 && row % 2 != 0){
//			  comp.setBackground(Color.BLUE);
//			  comp.setForeground(Color.RED);
//		  }
//		  else if(column == 2 && row % 2 == 0){
//			  comp.setBackground(Color.GREEN);
//			  comp.setForeground(Color.RED);
//		  }
//		  else if(column == 2 && row % 2 != 0){
//			  comp.setBackground(Color.WHITE);
//			  comp.setForeground(Color.RED);
//		  }
		  else{
			  comp.setBackground(Color.WHITE); 
			  comp.setForeground(Color.black);  
		  }
		  
		  return( comp ); 
		 } 
		} 


	public void closing(){
		pm.closeChulList();
		pm.cl = null;
	}

}

